home *** CD-ROM | disk | FTP | other *** search
/ Archive Magazine CD 1995 / Archive Magazine CD 1995.iso / discs / shareware / share_43 / source / c / OBJECTS < prev    next >
Encoding:
Text File  |  1991-08-22  |  12.2 KB  |  415 lines

  1. /*****************************************************************************
  2. *
  3. *                                    objects.c
  4. *
  5. *   from DKBTrace (c) 1990  David Buck
  6. *
  7. *  This module implements the methods for objects and composite objects.
  8. *
  9. * This software is freely distributable. The source and/or object code may be
  10. * copied or uploaded to communications services so long as this notice remains
  11. * at the top of each file.  If any changes are made to the program, you must
  12. * clearly indicate in the documentation and in the programs startup message
  13. * who it was who made the changes. The documentation should also describe what
  14. * those changes were. This software may not be included in whole or in
  15. * part into any commercial package without the express written consent of the
  16. * author.  It may, however, be included in other public domain or freely
  17. * distributed software so long as the proper credit for the software is given.
  18. *
  19. * This software is provided as is without any guarantees or warranty. Although
  20. * the author has attempted to find and correct any bugs in the software, he
  21. * is not responsible for any damage caused by the use of the software.  The
  22. * author is under no obligation to provide service, corrections, or upgrades
  23. * to this package.
  24. *
  25. * Despite all the legal stuff above, if you do find bugs, I would like to hear
  26. * about them.  Also, if you have any comments or questions, you may contact me
  27. * at the following address:
  28. *
  29. *     David Buck
  30. *     22C Sonnet Cres.
  31. *     Nepean Ontario
  32. *     Canada, K2H 8W7
  33. *
  34. *  I can also be reached on the following bulleton boards:
  35. *
  36. *     ATX              (613) 526-4141
  37. *     OMX              (613) 731-3419
  38. *     Mystic           (613) 731-0088 or (613) 731-6698
  39. *
  40. *  Fidonet:   1:163/109.9
  41. *  Internet:  David_Buck@Carleton.CA
  42. *
  43. *  IBM Port by Aaron A. Collins. Aaron may be reached on the following BBS'es:
  44. *
  45. *     Lattice BBS                      (708) 916-1200
  46. *     The Information Exchange BBS     (708) 945-5575
  47. *     Stillwaters BBS                  (708) 403-2826
  48. *
  49. *****************************************************************************/
  50.  
  51.  
  52. #include "frame.h"
  53. #include "vector.h"
  54. #include "dkbproto.h"
  55.  
  56. extern RAY *VP_Ray;
  57. extern long Bounding_Region_Tests, Bounding_Region_Tests_Succeeded;
  58.  
  59. METHODS Composite_Methods =
  60.    { Object_Intersect, All_Composite_Intersections,
  61.      Inside_Composite_Object, NULL,
  62.      Copy_Composite_Object,
  63.      Translate_Composite_Object, Rotate_Composite_Object,
  64.      Scale_Composite_Object, Invert_Composite_Object};
  65.  
  66. METHODS Basic_Object_Methods =
  67.    { Object_Intersect, All_Object_Intersections,
  68.      Inside_Basic_Object, NULL,
  69.      Copy_Basic_Object,
  70.      Translate_Basic_Object, Rotate_Basic_Object,
  71.      Scale_Basic_Object, Invert_Basic_Object};
  72.  
  73.  
  74. INTERSECTION *Object_Intersect (Object, Ray)
  75.    OBJECT *Object;
  76.    RAY *Ray;
  77.    {
  78.    INTERSECTION *Local_Intersection, *Queue_Element;
  79.    PRIOQ *Depth_Queue;
  80.  
  81.    Depth_Queue = pq_new (128);
  82.  
  83.    if ((All_Intersections (Object, Ray, Depth_Queue))
  84.       && ((Queue_Element = pq_get_highest (Depth_Queue)) != NULL))
  85.       {
  86.       if ((Local_Intersection = (INTERSECTION *) malloc(sizeof(INTERSECTION)))
  87.           == NULL) {
  88.       printf("Cannot allocate memory for local intersection\n");
  89.       exit(1);
  90.       }
  91.       Local_Intersection -> Point = Queue_Element -> Point;
  92.       Local_Intersection -> Shape = Queue_Element -> Shape;
  93.       Local_Intersection -> Depth = Queue_Element -> Depth;
  94.       Local_Intersection -> Object = Queue_Element -> Object;
  95.       pq_free (Depth_Queue);
  96.       return (Local_Intersection);
  97.       }
  98.    else
  99.       {
  100.       pq_free (Depth_Queue);
  101.       return (NULL);
  102.       }
  103.    }
  104.  
  105. int All_Composite_Intersections (Object, Ray, Depth_Queue)
  106.    OBJECT *Object;
  107.    RAY *Ray;
  108.    PRIOQ *Depth_Queue;
  109.    {
  110.    register int Intersection_Found;
  111.    SHAPE *Bounding_Shape;
  112.    INTERSECTION *Local_Intersection;
  113.    OBJECT *Local_Object;
  114.  
  115.    for (Bounding_Shape = ((COMPOSITE *) Object) -> Bounding_Shapes ;
  116.         Bounding_Shape != NULL ;
  117.         Bounding_Shape = Bounding_Shape -> Next_Object) {
  118.  
  119.      Bounding_Region_Tests++;
  120.      if ((Local_Intersection = Intersection ((OBJECT *) Bounding_Shape, Ray)) != NULL)
  121.         free (Local_Intersection);
  122.      else
  123.         if (!Inside (&Ray -> Initial, (OBJECT *) Bounding_Shape))
  124.            return (FALSE);
  125.      Bounding_Region_Tests_Succeeded++;
  126.      }
  127.  
  128.    Intersection_Found = FALSE;
  129.    for (Local_Object = ((COMPOSITE *) Object) -> Objects ;
  130.         Local_Object != NULL ;
  131.         Local_Object = Local_Object -> Next_Object)
  132.  
  133.       if (All_Intersections (Local_Object, Ray, Depth_Queue))
  134.          Intersection_Found = TRUE;
  135.  
  136.    return (Intersection_Found);
  137.    }
  138.  
  139. int All_Object_Intersections (Object, Ray, Depth_Queue)
  140.    OBJECT *Object;
  141.    RAY *Ray;
  142.    PRIOQ *Depth_Queue;
  143.    {
  144.    INTERSECTION *Local_Intersection;
  145.    SHAPE *Bounding_Shape;
  146.  
  147.    for (Bounding_Shape = Object -> Bounding_Shapes ;
  148.         Bounding_Shape != NULL ;
  149.         Bounding_Shape = Bounding_Shape -> Next_Object) {
  150.  
  151.       Bounding_Region_Tests++;
  152.       if ((Local_Intersection = Intersection ((OBJECT *) Bounding_Shape, Ray)) != NULL)
  153.          free (Local_Intersection);
  154.       else
  155.          if (!Inside (&Ray -> Initial, (OBJECT *) Bounding_Shape))
  156.             return (FALSE);
  157.       Bounding_Region_Tests_Succeeded++;
  158.       }
  159.  
  160.    All_Intersections ((OBJECT *)Object->Shape, Ray, Depth_Queue);
  161.  
  162.    if (pq_is_empty (Depth_Queue))
  163.      return (FALSE);
  164.    return (TRUE);
  165.    }
  166.  
  167.  
  168. int Inside_Basic_Object (Point, Object)
  169.    VECTOR *Point;
  170.    OBJECT *Object;
  171.    {
  172.    SHAPE *Bounding_Shape;
  173.  
  174.    for (Bounding_Shape = Object -> Bounding_Shapes ;
  175.         Bounding_Shape != NULL ;
  176.         Bounding_Shape = Bounding_Shape -> Next_Object)
  177.  
  178.       if (!Inside (Point, (OBJECT *) Bounding_Shape))
  179.          return (FALSE);
  180.  
  181.    if (Inside (Point, (OBJECT *) Object -> Shape))
  182.       return (TRUE);
  183.    return (FALSE);
  184.    }
  185.  
  186. int Inside_Composite_Object (Point, Object)
  187.    VECTOR *Point;
  188.    OBJECT *Object;
  189.    {
  190.    SHAPE *Bounding_Shape;
  191.    OBJECT *Local_Object;
  192.  
  193.    for (Bounding_Shape = ((COMPOSITE *) Object) -> Bounding_Shapes ;
  194.         Bounding_Shape != NULL ;
  195.         Bounding_Shape = Bounding_Shape -> Next_Object)
  196.  
  197.      if (!Inside (Point, (OBJECT *) Bounding_Shape))
  198.        return (FALSE);
  199.  
  200.    for (Local_Object = ((COMPOSITE *) Object) -> Objects ;
  201.         Local_Object != NULL ;
  202.         Local_Object = Local_Object -> Next_Object)
  203.  
  204.       if (Inside (Point, Local_Object))
  205.          return (TRUE);
  206.  
  207.    return (FALSE);
  208.    }
  209.  
  210. void *Copy_Basic_Object (Object)
  211.    OBJECT *Object;
  212.    {
  213.    SHAPE *Local_Shape, *Copied_Shape;
  214.    OBJECT *New_Object;
  215.  
  216.    New_Object = Get_Object();
  217.    *New_Object = *Object;
  218.    New_Object -> Next_Object = NULL;
  219.    New_Object -> Bounding_Shapes = NULL;
  220.    for (Local_Shape = Object -> Bounding_Shapes ;
  221.         Local_Shape != NULL ;
  222.         Local_Shape = Local_Shape -> Next_Object) {
  223.  
  224.       Copied_Shape = (SHAPE *) Copy((OBJECT *) Local_Shape);
  225.       Link ((OBJECT *) Copied_Shape,
  226.             (OBJECT **) &(Copied_Shape -> Next_Object),
  227.             (OBJECT **) &(New_Object -> Bounding_Shapes));
  228.       Copied_Shape -> Parent_Object = Object;
  229.       }
  230.  
  231.    New_Object -> Shape = (SHAPE *) Copy((OBJECT *) Object -> Shape);
  232.    Object -> Shape -> Parent_Object = Object;
  233.    return (New_Object);
  234.    }
  235.  
  236. void *Copy_Composite_Object (Object)
  237.    OBJECT *Object;
  238.    {
  239.    COMPOSITE *New_Object;
  240.    SHAPE *Local_Shape;
  241.    OBJECT *Local_Object, *Copied_Object;
  242.  
  243.    New_Object = Get_Composite_Object();
  244.    *New_Object = *((COMPOSITE *) Object);
  245.    New_Object -> Next_Object = NULL;
  246.    New_Object -> Objects = NULL;
  247.    for (Local_Object = ((COMPOSITE *) Object) -> Objects;
  248.         Local_Object != NULL ;
  249.         Local_Object = Local_Object -> Next_Object) {
  250.  
  251.       Copied_Object = (OBJECT *) Copy(Local_Object);
  252.       Link (Copied_Object,
  253.             &(Copied_Object -> Next_Object),
  254.             &(New_Object -> Objects));
  255.       }
  256.  
  257.    New_Object -> Bounding_Shapes = NULL;
  258.    for (Local_Shape = ((COMPOSITE *) Object) -> Bounding_Shapes;
  259.         Local_Shape != NULL ;
  260.         Local_Shape = Local_Shape -> Next_Object) {
  261.  
  262.       Copied_Object = (OBJECT *) Copy((OBJECT *) Local_Shape);
  263.       Link (Copied_Object,
  264.             &(Copied_Object -> Next_Object),
  265.             (OBJECT **) &(New_Object -> Bounding_Shapes));
  266.       }
  267.    return (New_Object);
  268.    }
  269.  
  270. void Translate_Basic_Object (Object, Vector)
  271.    OBJECT *Object;
  272.    VECTOR *Vector;
  273.    {
  274.    SHAPE *Local_Shape;
  275.  
  276.    for (Local_Shape = Object -> Bounding_Shapes ;
  277.         Local_Shape != NULL ;
  278.         Local_Shape = Local_Shape -> Next_Object)
  279.  
  280.       Translate ((OBJECT *) Local_Shape, Vector);
  281.  
  282.    Translate ((OBJECT *) Object -> Shape, Vector);
  283.  
  284.    VAdd (Object -> Object_Center, Object -> Object_Center, *Vector);
  285.    }
  286.  
  287. void Rotate_Basic_Object (Object, Vector)
  288.    OBJECT *Object;
  289.    VECTOR *Vector;
  290.    {
  291.    SHAPE *Local_Shape;
  292.    TRANSFORMATION Transformation;
  293.  
  294.    for (Local_Shape = Object -> Bounding_Shapes ;
  295.         Local_Shape != NULL ;
  296.         Local_Shape = Local_Shape -> Next_Object)
  297.  
  298.       Rotate ((OBJECT *) Local_Shape, Vector);
  299.  
  300.    Rotate ((OBJECT *) Object -> Shape, Vector);
  301.    Get_Rotation_Transformation (&Transformation, Vector);
  302.    MTransformVector (&Object->Object_Center,
  303.                      &Object->Object_Center, &Transformation);
  304.    }
  305.  
  306. void Scale_Basic_Object (Object, Vector)
  307.    OBJECT *Object;
  308.    VECTOR *Vector;
  309.    {
  310.    SHAPE *Local_Shape;
  311.  
  312.    for (Local_Shape = Object -> Bounding_Shapes ;
  313.         Local_Shape != NULL ;
  314.         Local_Shape = Local_Shape -> Next_Object)
  315.  
  316.       Scale ((OBJECT *) Local_Shape, Vector);
  317.  
  318.    Scale ((OBJECT *) Object -> Shape, Vector);
  319.  
  320.    VEvaluate (Object -> Object_Center, Object -> Object_Center, *Vector);
  321.    }
  322.  
  323. void Translate_Composite_Object (Object, Vector)
  324.    OBJECT *Object;
  325.    VECTOR *Vector;
  326.    {
  327.    OBJECT *Local_Object;
  328.    SHAPE *Local_Shape;
  329.  
  330.    for (Local_Object = ((COMPOSITE *) Object) -> Objects;
  331.         Local_Object != NULL ;
  332.         Local_Object = Local_Object -> Next_Object)
  333.  
  334.       Translate (Local_Object, Vector);   
  335.  
  336.    for (Local_Shape = ((COMPOSITE *) Object) -> Bounding_Shapes ;
  337.         Local_Shape != NULL ;
  338.         Local_Shape = Local_Shape -> Next_Object)
  339.  
  340.       Translate ((OBJECT *) Local_Shape, Vector);
  341.    }
  342.  
  343. void Rotate_Composite_Object (Object, Vector)
  344.    OBJECT *Object;
  345.    VECTOR *Vector;
  346.    {
  347.    OBJECT *Local_Object;
  348.    SHAPE *Local_Shape;
  349.  
  350.    for (Local_Object = ((COMPOSITE *) Object) -> Objects;
  351.         Local_Object != NULL ;
  352.         Local_Object = Local_Object -> Next_Object)
  353.  
  354.       Rotate (Local_Object, Vector);   
  355.  
  356.    for (Local_Shape = ((COMPOSITE *) Object) -> Bounding_Shapes ;
  357.         Local_Shape != NULL ;
  358.         Local_Shape = Local_Shape -> Next_Object)
  359.  
  360.       Rotate ((OBJECT *) Local_Shape, Vector);
  361.    }
  362.  
  363. void Scale_Composite_Object (Object, Vector)
  364.    OBJECT *Object;
  365.    VECTOR *Vector;
  366.    {
  367.    OBJECT *Local_Object;
  368.    SHAPE *Local_Shape;
  369.  
  370.    for (Local_Object = ((COMPOSITE *) Object) -> Objects;
  371.         Local_Object != NULL ;
  372.         Local_Object = Local_Object -> Next_Object)
  373.  
  374.       Scale (Local_Object, Vector);   
  375.  
  376.    for (Local_Shape = ((COMPOSITE *) Object) -> Bounding_Shapes ;
  377.         Local_Shape != NULL ;
  378.         Local_Shape = Local_Shape -> Next_Object)
  379.  
  380.       Scale ((OBJECT *) Local_Shape, Vector);
  381.    }
  382.  
  383.  
  384. void Invert_Basic_Object (Object)
  385.    OBJECT *Object;
  386.    {
  387.    SHAPE *Local_Shape;
  388.  
  389.    for (Local_Shape = Object -> Bounding_Shapes ;
  390.         Local_Shape != NULL ;
  391.         Local_Shape = Local_Shape -> Next_Object)
  392.  
  393.       Invert ((OBJECT *) Local_Shape);
  394.    Invert ((OBJECT *) Object -> Shape);
  395.    }
  396.  
  397. void Invert_Composite_Object (Object)
  398.    OBJECT *Object;
  399.    {
  400.    OBJECT *Local_Object;
  401.    SHAPE *Local_Shape;
  402.  
  403.    for (Local_Object = ((COMPOSITE *)Object) -> Objects;
  404.         Local_Object != NULL ;
  405.         Local_Object = Local_Object -> Next_Object)
  406.  
  407.       Invert (Local_Object);   
  408.  
  409.    for (Local_Shape = ((COMPOSITE *) Object) -> Bounding_Shapes ;
  410.         Local_Shape != NULL ;
  411.         Local_Shape = Local_Shape -> Next_Object)
  412.  
  413.       Invert ((OBJECT *) Local_Shape);
  414.    }
  415.